A heap zone is a heap (that is, an area in which you can dynamically allocate and release memory on demand) together with a zone header and a zone trailer. The zone header is an area of memory that contains essential information about the heap, such as the number of free bytes in the heap and the addresses of the heap's grow-zone function and purge-warning procedure. The zone trailer is just a minimum-sized block placed as a marker at the end of the heap zone. (See "Heap Zones" for a complete description of zone headers and trailers.)
When your application is executing, there exist at least two heap zones: your application's heap zone (created when your application was launched) and the system heap zone (created when the system was started up). The system heap zone is the heap zone that contains the system heap. Your application heap zone (also known as the original application heap zone ) is the heap zone initially provided by the Memory Manager for use by your application and any system software routines your application calls.
Ordinarily, you allocate and release blocks of memory in the current heap zone, which by default is your application heap zone. Unless you change the current heap zone (for example, by calling the InitZone or SetZone procedures), you do not need to worry about which is the current zone; all blocks that you access are taken from the current heap zone, that is, your application heap zone.
Occasionally, however, you might need to allocate memory in the system heap zone. System software uses the system heap to store information it needs. Although, in general, you should not allocate memory in the system heap, there are several valid reasons for doing so. First, if you are implementing a system extension, the extension can use the system heap to store information. Second, if you want the Time Manager or Vertical Retrace Manager to execute some interrupt code when your application is not the current application, you might in certain cases need to store the task record and the task code in the system heap. Third, if you write interrupt code that itself uses heap memory, you should either place that memory in the system heap or hold it in real RAM to prevent page faults at interrupt time, as discussed in the chapter "Virtual Memory Manager" in this book.
You can create additional heap zones for your application's own use by calling the InitZone procedure. If you do maintain more than one heap zone, you can find out which heap zone is the current one at any time by calling the GetZone function, and you can switch zones by calling the SetZone procedure. Almost all Memory Manager operations implicitly apply to the current heap zone. To refer to the system heap zone or to the (original) application heap zone, you can call the functions SystemZone or ApplicationZone . To find out which zone a particular block resides in, you can call the HandleZone function (if the block is relocatable) or the PtrZone function (if it's nonrelocatable).
Be sure, when calling routines that access blocks, that the zone in which the block is located is the current zone. If, for example, you attempt to release an empty resource in the system zone when the current zone is not the system zone, the Operating System might incorrectly update the list of free master pointers in your partition.
Once you have created a heap zone, it remains fixed in size and location. For this reason, it usually makes more sense to use the undivided application heap zone for all of your memory-allocation needs. You might, however, choose to initialize an additional heap zone in circumstances like these:
Before deciding to create additional heap zones, however, make sure that you really need to. Maintaining multiple heap zones requires a considerable amount of extra work. You must always make sure to allocate or release memory in the correct zone, and you must monitor memory conditions in each zone so that your application doesn't run out of memory.